package com.benyi.util;

 
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class Mail { 
    
    public static void sendMail(String smtpName,String port,String fromMail,String passwd,String toMail,
    		String subject,String content) throws Exception {
    	Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", smtpName);
        properties.put("mail.smtp.port", port);
 
        // 创建会话
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromMail, passwd);
            }
        });

       // 创建邮件对象
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromMail)); // 发件人地址
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toMail)); // 收件人地址
        message.setSubject(subject); // 邮件主题 
        message.setContent(content, "text/html; charset=utf-8"); 
        // 发送邮件
        Transport.send(message);
        System.out.println("HTML邮件发送成功！"); 
    } 
    
   
}
